﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HandyControl.Controls;
using Model;
 
using RestSharp;
using RestSharp.Authenticators;
namespace UI.View.Users
{
    /// <summary>
    /// List.xaml 的交互逻辑
    /// </summary>
    public partial class List : Page
    {
        RestClient client = new RestClient(new Uri(Comm.TempData.BaseUrl));

        ViewModel.UserViewModel vm;
        public List()
        {
            InitializeComponent();

            vm = DataContext as ViewModel.UserViewModel;

            vm.CurrUser = new U_User();
            LoadData();

            


        }

        PmsApi api = new PmsApi();

        private async  void LoadData()
        {
            var request = new RestRequest("user", RestSharp.DataFormat.Json);


           
            vm.List = await api.GetList<U_User>(request);


            //var resp = client.Execute(request);

            var req = new RestRequest("user/1000",Method.GET);
            var u = await api.Get<U_User>(req);
            //HandyControl.Controls.MessageBox.Show(u.Name);
            //var rep = await client.ExecuteAsync<ListDataResponse<U_User>>(request);
            //vm.List = rep.Data.data;
            //var obj = await api.Execute<U_User>(request);
        }


        private async void LoadByID()
        {
            var request = new RestRequest("user/1000", RestSharp.DataFormat.Json);
            //var response = await client.GetAsync<Data<U_User>>(request);

            //MessageBox.Show(response.data.Name);

            var api = new PmsApi();
            vm.CurrUser  = await api.Get<U_User>(request);
            //MessageBox.Show(vm.CurrUser.Name);
        }
        private void btn_Create_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = true;
            vm.CurrUser= new U_User();
            vm.IsEdit = false;
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = false;
        }

        private async void btn_save_click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty( vm.CurrUser.Name))
            {
                Growl.WarningGlobal("用户名称不能为空！");
                return;
            }


            if(vm.IsEdit)
            {
                if (vm.CurrUser != null)
                {
                    var res = new RestRequest("user", Method.PUT);
                    res.AddJsonBody(vm.CurrUser);
                    var api = new PmsApi();
                   var isSuccess =  await client.ExecuteAsync<SingleDataResponse<int>>(res);
                    if(isSuccess.Data.data>0)
                    {
                         Growl.SuccessGlobal("修改成功！");
                    }
                    else
                    {
                        Growl.SuccessGlobal("修改失败！");
                    }
                }
            }
            else
            {
                if (vm.CurrUser != null)
                {
                    var res = new RestRequest("user", Method.POST);
                    res.AddJsonBody(vm.CurrUser);
                    var api = new PmsApi();
                   var isSuccess = await api.Post<U_User>(res);
                    if (isSuccess > 0)
                    {
                        Growl.SuccessGlobal("保存成功！");
                    }
                    else
                    {
                        Growl.SuccessGlobal("保存失败！");
                    }
                }
            }
            

            LoadData();
        }

        private async void btn_Delete_Clicl(object sender, RoutedEventArgs e)
        {
           // var btn = sender as Button;
           // MessageBox.Show(vm.CurrUser.Id.ToString());

            var res = new RestRequest("user/" + vm.CurrUser.Id, Method.DELETE);
            //res.AddParameter("id",vm.CurrUser.Id);
            var api = new PmsApi();
           var ret = await api.Delete<bool>(res);
            if(ret)
            {
                HandyControl.Controls.Growl.SuccessGlobal("删除成功！");
            }
            else
            {
                HandyControl.Controls.Growl.ErrorGlobal("删除失败！");
            }

            LoadData();
        }

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = true;
            
            vm.IsEdit = true;
        }
    }
}
